package cst.a931xmc.yuejian20.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cst.a931xmc.yuejian20.Activity.AorderActivity;
import cst.a931xmc.yuejian20.Activity.TorderActivity;
import cst.a931xmc.yuejian20.Bean.ActivityOrderBean;
import cst.a931xmc.yuejian20.Bean.TrainOrderBean;
import cst.a931xmc.yuejian20.Model.ApiModel;
import cst.a931xmc.yuejian20.R;

public class MyOrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private LayoutInflater inflater;
    private List<ActivityOrderBean> list1;
    private List<TrainOrderBean> list2;
    private Context context;

    public MyOrderAdapter(Context context){
        inflater = LayoutInflater.from(context);
        this.context = context;
    }

    public void setAOrderList(List<ActivityOrderBean> l) {
        list1 = l;
        notifyDataSetChanged();
    }

    public void setTOrderList(List<TrainOrderBean> l) {
        list2 = l;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = inflater.from(parent.getContext()).inflate(R.layout.item_myorder, parent, false);
        RecyclerView.ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(list1!=null) {
            ActivityOrderBean bean1 = list1.get(position);
            if (bean1 == null)
                return;
            final ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.order_num.setText("x" + bean1.getAorder_num());
            viewHolder.order_price.setText("￥" + bean1.getAorder_price());
            viewHolder.item_name.setText(bean1.getActivity_name());
            viewHolder.item_intro.setText(bean1.getActivity_intro());
            viewHolder.order_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = viewHolder.getLayoutPosition();
                    Intent intent;
                    intent = new Intent(context, AorderActivity.class);
                    intent.putExtra("aorder_id", list1.get(position).getAorder_id());
                    context.startActivity(intent);
                }
            });
            ApiModel apiModel = new ApiModel();
            Picasso.with(context)
                    .load(apiModel.getBaseUrl() + "static/images/" + bean1.getActivity_pic())
                    .placeholder(R.drawable.nopic)
                    .into(viewHolder.item_pic);
        }
        if(list2!=null){
            TrainOrderBean bean2 = list2.get(position);
            if(bean2 == null)
                return;
            final ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.order_num.setText("x" + bean2.getTorder_num());
            viewHolder.order_price.setText("￥" + bean2.getTorder_price());
            viewHolder.item_name.setText(bean2.getTrain_name());
            viewHolder.item_intro.setText(bean2.getTrain_intro());
            viewHolder.order_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = viewHolder.getLayoutPosition();
                    Intent intent;
                    intent = new Intent(context, TorderActivity.class);
                    intent.putExtra("torder_id", list2.get(position).getTorder_id());
                    context.startActivity(intent);
                }
            });
            ApiModel apiModel = new ApiModel();
            Picasso.with(context)
                    .load(apiModel.getBaseUrl() + "static/images/" + bean2.getTrain_pic())
                    .placeholder(R.drawable.nopic)
                    .into(viewHolder.item_pic);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (list1 == null){
            if (list2 == null)
                return 0;
            else return list2.size();
        }
        else return list1.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView item_pic;
        private TextView item_name;
        private TextView item_intro;
        private TextView order_num;
        private TextView order_price;
        private LinearLayout order_item;

        public ViewHolder(View itemView) {
            super(itemView);
            item_pic = itemView.findViewById(R.id.item_pic);
            item_name = itemView.findViewById(R.id.item_name);
            item_intro = itemView.findViewById(R.id.item_intro);
            order_num = itemView.findViewById(R.id.order_num);
            order_price = itemView.findViewById(R.id.order_price);
            order_item = itemView.findViewById(R.id.order_item);
        }
    }
}
